package coms.dataaccess.hibernate;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.registry.infomodel.User;

import coms.dataaccess.UserDao;
import coms.datatransfer.FileItemDTO;
import coms.datatransfer.RoleDTO;
import coms.datatransfer.UserDTO;

public class UserDaoImpl extends AbstractDAO implements UserDao{
	
	
	
	public UserDaoImpl(){
		super();
	}
	

	@Override
	public void createUser(UserDTO usr) {
		usr.setListOfFileItems(new HashSet<FileItemDTO>());
		RoleDTO role = (RoleDTO) super.find(RoleDTO.class, 1);
		Set<UserDTO> set = role.getListOfUsers();
		set.add(usr);
		role.setListOfUsers(set);
		super.saveOrUpdate(role);
		
	}


	@Override
	public UserDTO validateUser(UserDTO usr) {
		// TODO Auto-generated method stub
		
		return (UserDTO) super.find(usr.getClass() , usr.getUserID());
		
	}


	@Override
	public UserDTO findUserByID(int userID) {
		UserDTO user = new UserDTO();
		user = (UserDTO) super.find(UserDTO.class , userID);
		return user;
	}
	
	@Override
	public List<Object[]> findUserByRoleID(int roleID){
		
      
	    return (ArrayList<Object[]>)super.manyToManyfindById(UserDTO.class,"roleDTO","roleID", Integer.toString(roleID));
		
	}
	

}
